/**
 * 
 */
package com.gpms.service.Topics;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gpms.bean.InstituteProfessional;
import com.gpms.bean.Suggest;
import com.gpms.bean.Teacher;
import com.gpms.bean.Topics;
import com.gpms.bean.TopicsProfessional;
import com.gpms.dao.HibernateDaoFactory;
import com.gpms.dao.IHibernateDao;
import com.gpms.dao.ITopicsDao;
import com.gpms.service.common.PublicOperate;
import com.gpms.service.common.PublicOperateFactory;

/**
 * @author laborc
 *
 */
public class AutidingTopicsOperate implements IAutidingTopicsOperate {

	public String distribute(List<Topics> topics,Teacher teacher) {
		IHibernateDao dao = HibernateDaoFactory.createDao("topicsDao");
		if(topics!=null)
		{
			for(Topics t:topics)
			{
				t.setAutiding(teacher);
			}
			try {
				dao.save(topics);
			} catch (Exception e) {
				e.printStackTrace();
				return "input";
			}
		}else{
			List<Teacher> teacherr=new ArrayList<Teacher>();
			teacherr.add(teacher);
			try {
				dao.save(teacherr);
			} catch (Exception e) {
				e.printStackTrace();
				return "input";
			}
		}
		
		
		return "success";
	}


	public Topics showTopicsInfo(int topicsNum) {
		String hql="from Topics t where t.topicsNum="+topicsNum+"";
		IHibernateDao dao = HibernateDaoFactory.createDao("topicsDao");
		Topics topics=new Topics();
		topics.setTopicsNum(topicsNum);
		topics = (Topics)dao.selectById(topics);
		return topics;
	}

	public int showTopicsNum(String num) {
		String hql="select count(*) from Topics t where t.autiding.teacherNum='"+num+"' and t.canDo='0' and t.autidingState='0'";
		IHibernateDao dao = HibernateDaoFactory.createDao("topicsDao");
		int total = ((Long)dao.selectByHql(hql).iterator().next()).intValue();
		return total;
	}

	public String autiding(Topics topics) {
		Date date = topics.getAutidingTime();
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd"); 
		String datetime = tempDate.format(date);
		String hql="update Topics t set t.autidingState='"+topics.getAutidingState()+"' ,t.autidingTime=to_date('"+datetime+"','yyyy-mm-dd') where t.topicsNum="+topics.getTopicsNum()+"";
		IHibernateDao dao = HibernateDaoFactory.createDao("topicsDao");
		String result;
		try{
		result= dao.modify(hql);
		}catch(Exception e)
		{
			e.printStackTrace();
			return "input";
		}
		return result;
	}


	public String back(Topics topics,Suggest suggest) 
	{
		String result;
		ITopicsDao dao = (ITopicsDao)HibernateDaoFactory.createDao("topicsDao");
		result = dao.back(topics, suggest);
		return result;
	}


	public String distract(Topics topics) {
		String hql="from TopicsProfessional t where t.topics.topicsNum="+topics.getTopicsNum()+" and t.canDo='0' order by t.PRI";
		ITopicsDao dao = (ITopicsDao)HibernateDaoFactory.createDao("topicsDao");
		List<Object> list = dao.selectByHql(hql);
		if(list.isEmpty())
		{
			return "false";
		}
		TopicsProfessional t=(TopicsProfessional)list.get(0);
		PublicOperate operate = PublicOperateFactory.createOperate("common");
		String ip = operate.getInstituteNum(t.getProfessional().getProfessionalNum());
		String updateTP="update TopicsProfessional tp set tp.canDo='1' where tp.TopicsProfessionalNum="+t.getTopicsProfessionalNum()+"";
		String thql="update Topics t set t.institute.instituteNum='"+ip+"' ,t.autiding.teacherNum='' ,t.distributeState='0' ,t.autidingState='0',t.professional.professionalNum='"+t.getProfessional().getProfessionalNum()+"' where t.topicsNum="+topics.getTopicsNum()+"";
		String result = dao.distract(updateTP,thql);
		return result;
	}


}
